/**
 * 常用工具类
 */
J.Util = (function($){
    var parseHash = function(hash){
        var tag,query,param={};
        var arr = hash.split('?');
        tag = arr[0];
        if(arr.length>1){
            var seg,s;
            query = arr[1];
            seg = query.split('&');
            for(var i=0;i<seg.length;i++){
                if(!seg[i])continue;
                s = seg[i].split('=');
                param[s[0]] = s[1];
            }
        }
        return {
            hash : hash,
            tag : tag,
            query : query,
            param : param
        }
    }

    /**
     * 格式化date
     * @param date
     * @param format
     */
    var formatDate = function(date,format){
    	if(typeof date=="number"){
    		date=new Date(date);
    	}
        var o =
        {
            "M+" : date.getMonth()+1, //month
            "d+" : date.getDate(),    //day
            "h+" : date.getHours(),   //hour
            "m+" : date.getMinutes(), //minute
            "s+" : date.getSeconds(), //second
            "q+" : Math.floor((date.getMonth()+3)/3),  //quarter
            "S" : date.getMilliseconds() //millisecond
        }
        if(/(y+)/.test(format))
            format=format.replace(RegExp.$1,(date.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(format))
                format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
        return format;
    }
    
	var fmt = "yyyy-MM-dd",
		today=formatDate(new Date(),fmt),
		today_num=Number(new Date(today)),
		oneday = 1000 * 24 * 60 * 60;
		
	var arr = today.split("-"),
		year = arr[0],
		month = Number(arr[1]) - 1,
		day = Number(arr[2]);
	var week = Number((new Date(today)).getDay());
	
	week = week == 0 ? 7 : week;

	var Dates = {
		"-1":",",
		"0": today + "," +today, //今天
		"1": formatDate(Number(new Date(today))- oneday, fmt) + "," + formatDate(Number(new Date(today)) - oneday, fmt),//昨天
		"2": formatDate(new Date(year, month, day - week + 1), fmt) + "," + formatDate(new Date(year, month, day + 6 - week + 1), fmt), //本周
		"3":formatDate(new Date(year, month, day - 6), fmt) + "," +today,//最近7天
		"4":formatDate(new Date(year, month, day - 30), fmt) + "," +today,//最近30天
		"5": formatDate(new Date(year, month, 1), fmt) + "," + formatDate(new Date(year, month + 1, 1) - oneday, fmt), //本月
		"6": formatDate(new Date(year, month - 1, 1), fmt) + "," + formatDate(new Date(year, month, 1) - oneday, fmt), //上月
//		"3": formatDate(new Date(year, month, day - week - 6), fmt) + "," + formatDate(new Date(year, month, day + 6 - week - 6), fmt), //上周
//		"4": formatDate(new Date(year, month, day - week + 8), fmt) + "," + formatDate(new Date(year, month, day + 6 - week + 8), fmt), //下周
	}
    var getDatesByCode=function(code){
    	var d=Dates[code].split(",");
    	return {
    		sdate:d[0],
    		edate:d[1]
    	};
    }

    return {
        parseHash : parseHash,
        formatDate : formatDate,
        getDatesByCode:getDatesByCode
    }

})(J.$);